/***************************************************************************************************
 *
 * Project: Utilities
 *
 * Class: LoggerUtils
 *
 * File name: LoggerUtils.java
 *
 * Aug 6, 2007
 *
 **************************************************************************************************/

package com.eastmountain.util.logging;

import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LoggerUtils {

    public LoggerUtils() {

    }

    /***********************************************************************************************
     *
     *  setUpLogger
     *
     **********************************************************************************************/

    public static Logger setUpLogger( String projectName ) {

        Logger logger = createLogger( projectName, null );

        return( logger );

    }

    /***********************************************************************************************
     *
     *  setUpLogger
     *
     **********************************************************************************************/

    public static Logger setUpLogger( String projectName, Level level ) {

        Logger logger = createLogger( projectName, level );

        return( logger );

    }

    /***********************************************************************************************
     *
     *  createLogger
     *
     **********************************************************************************************/

    private static Logger createLogger( String projectName, Level level ) {

        Logger logger = Logger.getLogger( projectName );

        if ( level != null ) {

            logger.setLevel( level );

        }

        BrieferFormatter brieferFormatter;
        String fileName = "C:\\Documents and Settings\\All Users\\Documents\\"
                                        + "Development\\Eclipse3.3\\logs\\"
                                        + projectName + ".html";

        FileHandler fileHandler = null;
        ConsoleHandler consoleHandler = null;

        try {

            fileHandler = new FileHandler( fileName, 1000000, 20 );
            fileHandler.setEncoding( "UTF-8" );

            consoleHandler = new ConsoleHandler();
            consoleHandler.setEncoding( "UTF-8" );

        } catch ( SecurityException e ) {

            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch ( IOException e ) {

            // TODO Auto-generated catch block
            e.printStackTrace();

        }

        brieferFormatter =
            new LoggerUtils.BrieferFormatter();

        fileHandler.setFormatter( brieferFormatter );

        logger.addHandler( fileHandler );
        //logger.addHandler( consoleHandler );

        logger.info( "Logger created with fileName: " + fileName );

        return (logger);

    }

    static class BrieferFormatter extends SimpleFormatter {

        public BrieferFormatter() {

        }

        @Override
        public String format( LogRecord record ) {

            String briefFormatterMessage = "";

            if ( record.getLevel() == Level.WARNING ) {

                briefFormatterMessage = record.getLevel() + " - " + record.getMessage();

            } else if ( record.getLevel() == LevelExtras.DEBUG_1 ) {

                System.out.println( "\n   ******** \n   ******** DEBUG_1 has gottne thisfar !!!\n   ******** " );
                briefFormatterMessage = super.format( record );

            } else {

                briefFormatterMessage = super.format( record );

            }

            return( briefFormatterMessage );

        }

    }

}
